package com.jcp.baselibrary.popup;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.PopupWindow;

/**
 * Created by jcp on 2017/6/23.
 */

public class CommonPopupWindow extends PopupWindow {

    public PopupController controller;

    @Override
    public int getWidth() {
        return controller.mPopupView.getMeasuredWidth();
    }

    @Override
    public int getHeight() {
        return controller.mPopupView.getMeasuredHeight();
    }


    public interface ViewInterface {
        void getChildView(PopupWindow popupWindow, View view, int layoutResId);
    }

    protected CommonPopupWindow(Activity activity) {
        controller = new PopupController(activity, this);
    }

    public void showAtLocation(final View view,  int gravity, float level) {
        showAtLocation(view, gravity , 0, 0, level);
    }


    public void showAtLocation(final View view, int gravity, int xOffset, int YOffset, float level) {
        controller.setBackGroundLevel(level);
        super.showAtLocation(view, gravity, xOffset, YOffset);
    }


    public void showAsDropDown(final View view, float level) {
        showAsDropDown(view, 0, 0, level);
    }


    public void showAsDropDown(final View view, int XOffset, int YOffset, float level) {
        controller.setBackGroundLevel(level);
        super.showAsDropDown(view, XOffset, YOffset);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        controller.setBackGroundLevel(1.0f);
    }


    public static class Builder {
        private final PopupController.PopupParams params;
        private ViewInterface listener;

        public Builder(Activity activity) {
            params = new PopupController.PopupParams(activity);
        }

        /**
         * @param layoutResId 设置PopupWindow 布局ID
         * @return Builder
         */
        public Builder setView(int layoutResId) {
            params.mView = null;
            params.layoutResId = layoutResId;
            return this;
        }

        /**
         * @param view 设置PopupWindow布局
         * @return Builder
         */
        public Builder setView(View view) {
            params.mView = view;
            params.layoutResId = 0;
            return this;
        }

        /**
         * 设置子View
         *
         * @param listener ViewInterface
         * @return Builder
         */
        public Builder setCreateViewCallback(ViewInterface listener) {
            this.listener = listener;
            return this;
        }

        /**
         * 设置宽度和高度 如果不设置 默认是wrap_content
         *
         * @param width 宽
         * @return Builder
         */
        public Builder setWidthAndHeight(int width, int height) {
            params.mWidth = width;
            params.mHeight = height;
            return this;
        }


        /**
         * 是否可点击Outside消失
         *
         * @param touchable 是否可点击
         * @return Builder
         */
        public Builder setOutsideTouchable(boolean touchable) {
            params.isTouchable = touchable;
            return this;
        }

        /**
         * 设置动画
         *
         * @return Builder
         */
        public Builder setAnimationStyle(int animationStyle) {
            params.isShowAnim = animationStyle != 0;
            params.animationStyle = animationStyle;
            return this;
        }

        public CommonPopupWindow create() {
            final CommonPopupWindow popupWindow = new CommonPopupWindow(params.mActivity);
            params.apply(popupWindow.controller);
            if (listener != null && params.layoutResId != 0) {
                listener.getChildView(popupWindow, popupWindow.controller.mPopupView, params.layoutResId);
            }
            measureWidthAndHeight(popupWindow.controller.mPopupView);
            return popupWindow;
        }

    }



    /**
     * 测量View的宽高
     *
     * @param view View
     */
    public static void measureWidthAndHeight(View view) {
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
    }
}
